/*
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "client_disc_service.h"

#include "client_disc_manager.h"
#include "disc_log.h"
#include "softbus_client_frame_manager.h"
#include "softbus_errcode.h"

static const char *g_dmPkgName = "ohos.distributedhardware.devicemanager";

static int32_t CommonInit(void)
{
    if (InitSoftBus(g_dmPkgName) != SOFTBUS_OK) {
        DISC_LOGE(DISC_SDK, "init softbus failed");
        return SOFTBUS_NO_INIT;
    }
    if (CheckPackageName(g_dmPkgName) != SOFTBUS_OK) {
        DISC_LOGE(DISC_SDK, "check packageName failed");
        return SOFTBUS_INVALID_PARAM;
    }
    return SOFTBUS_OK;
}

int32_t RegisterService(const ServiceInfo *serviceInfo)
{
    DISC_CHECK_AND_RETURN_RET_LOGE(serviceInfo != NULL, SOFTBUS_INVALID_PARAM, DISC_SDK, "serviceInfo is NULL");
    int32_t ret = CommonInit();
    if (ret != SOFTBUS_OK) {
        return ret;
    }
    return RegisterServiceInner(serviceInfo);
}

int32_t UnRegisterService(const char *serviceId)
{
    DISC_CHECK_AND_RETURN_RET_LOGE(serviceId != NULL, SOFTBUS_INVALID_PARAM, DISC_SDK, "serviceId is NULL");
    int32_t ret = CommonInit();
    if (ret != SOFTBUS_OK) {
        return ret;
    }
    return UnRegisterServiceInner(serviceId);
}

int32_t UpdateService(const ServiceInfo *serviceInfo)
{
    DISC_CHECK_AND_RETURN_RET_LOGE(serviceInfo != NULL, SOFTBUS_INVALID_PARAM, DISC_SDK, "serviceInfo is NULL");
    int32_t ret = CommonInit();
    if (ret != SOFTBUS_OK) {
        return ret;
    }
    return UpdateServiceInner(serviceInfo);
}
